/*****************************************************************************
* @brief provide system init routine/configuration for NV32Fxx.
*
*******************************************************************************/

#include "common.h"
#include "sim.h"
#include "sysinit.h"
#include "ics.h"

#define DISABLE_NMI
/********************************************************************/

uint16_t global_pass_count = 0;
uint16_t global_fail_count = 0;


void print_sys_log(void);
void UART_InitPrint(void);

/*****************************************************************************//*!
+FUNCTION----------------------------------------------------------------
* @function name: sysinit
*
* @brief  initalize system including SIM, ICS, UART, etc
*        
* @param  none 
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sysinit (void)
{
    SIM_ConfigType  sSIMConfig = {{0},0};
  //  ICS_ConfigType  sICSConfig = {0};
    
    /* initialize the Pass/Fail counts to 0 */
     global_pass_count = 0;
     global_fail_count = 0; 
		
     EFMCR &=0xFFFF1103; // set wait state 1

#if defined(TRIM_IRC)    
    /* if not trimmed, do trim first */
    ICS_Trim(ICS_TRIM_VALUE);
#endif  
    /* 
     * Enable SWD pin, RESET pin 
     */
    /* 
     * NOTE: please make sure other register bits are also write-once and
     * need add other bit mask here if needed.
     */
#if defined(SPI0_PINREMAP)        
    sSIMConfig.u32PinSel |= SIM_PINSEL_SPI0PS_MASK;
#endif

#if defined(OUTPUT_BUSCLK)
    sSIMConfig.sBits.bEnableCLKOUT = 1;      /* output bus clock if enabled */
#endif
    
#if defined(DISABLE_NMI)
    sSIMConfig.sBits.bDisableNMI = 1;
#endif
    
    #if !defined(CPU_NV32M3) 
    /* make sure clocks to peripheral modules are enabled */
    sSIMConfig.u32SCGC |= SIM_SCGC_SWD_MASK | SIM_SCGC_FLASH_MASK |
                         SIM_SCGC_UART0_MASK | SIM_SCGC_UART1_MASK | 
                         SIM_SCGC_UART2_MASK
                         ;
    #else
    sSIMConfig.u32SCGC |= SIM_SCGC_SWD_MASK | SIM_SCGC_FLASH_MASK |
                         SIM_SCGC_UART0_MASK 
                         ;
    #endif
    
    #if !defined(CPU_NV32)
    /* bus clock divided by 2 */
   // sSIMConfig.u32BusDiv |= SIM_CLKDIV_OUTDIV2_MASK;                           
    #endif

  //               sSIMConfig.sBits.bBusDiv |= SIM_BUSDIV_BUSDIV_MASK; 
		 
    SIM_Init(&sSIMConfig);                   /* initialize SIM */ 
		 

      
    
 }


void UART_InitPrint()
{
  
}
/*****************************************************************************//*!
+FUNCTION----------------------------------------------------------------
* @function name: print_sys_log
*
* @brief  print system reset sources
*        
* @param  none 
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void print_sys_log(void) 
{
   // uint8_t u8Rst = 0;
   // uint8_t u8FamID,u8SubFamID,u8RevID,u8PinID;
   // uint32_t u32Status;
    
}


/*****************************************************************************//*!
+FUNCTION----------------------------------------------------------------
* @function name: end_test
*
* @brief  print test result (pass/fail counts) after end of test
*        
* @param  none 
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void end_test(void)
{

  if(global_fail_count==0){
    printf("\n global_pass_count = 0x%02x%02x",(uint8_t)(global_pass_count>>8),(uint8_t)global_pass_count);
    printf("\n\n TEST PASSED");       
  } else{
    printf("\n global_pass_count = 0x%02x%02x",(uint8_t)(global_pass_count>>8),(uint8_t)global_pass_count);
    printf("\n global_fail_count = 0x%02x%02x",(uint8_t)(global_fail_count>>8),(uint8_t)global_fail_count);
    printf("\n\n TEST FAILED");
  }
  
  printf("\n\n TEST FINISHED");  
  
}

/*****************************************************************************//*!
+FUNCTION----------------------------------------------------------------
* @function name: UART_InitPrint
*
* @brief initialize UART for print on port defined by TERM_PORT.
*        
* @param  none 
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

